import { getMsgUnread } from '@/api/message'

const message = {
  namespaced: true,
  state: {
    unreadArr: [],
    unreadMap: {}
  },

  mutations: {
    SET_MSG_UNREAD (state, data) {
      state.unreadArr = data
      const map = {}
      data.forEach(item => {
        map[item.appId] = item.count
      })
      state.unreadMap = map
    },

    UPDATE_APP_UNREAD (state, data) {
      const { appId, count } = data
      state.unreadMap[appId] = count
    }
  },

  actions: {
    GET_MSG_UNREAD ({ commit }, toUser) {
      getMsgUnread({ toUser }).then(res => {
        commit('SET_MSG_UNREAD', res.data)
      })
    }
  }
}

export default message
